package com.changhai.diabetes.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.arcsoft.webframework.DispatcherAction;
import com.changhai.diabetes.model.AjaxResultMsg;
import com.changhai.diabetes.model.SessionUser;
import com.changhai.diabetes.model.TableForPatient;
import com.changhai.diabetes.service.CommonService;
import com.changhai.diabetes.util.Constants;
import com.changhai.diabetes.util.StringConvertor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author Andy
 * 
 */
public abstract class BaseAction extends DispatcherAction {
	private static final long serialVersionUID = 4510696924904493769L;
	private String userName;
	private AjaxResultMsg ajaxMsg = new AjaxResultMsg(Action.SUCCESS);
	public CommonService commonService;
	public List<TableForPatient> tbList;

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		if (StringUtils.isBlank(userName)) {
			SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
			if (sUser != null) {
				userName = sUser.getLoginName();
			}
		}
		return userName;
	}

	/**
	 * @return the id
	 */
	public Long getUserId() {
		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
		if (sUser != null) {
			return sUser.getId();
		} else {
			return 0L;
		}
	}

	public SessionUser getSessionUser() {
		return (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);
	}

	/**
	 * @return the ajaxMsg
	 */
	public AjaxResultMsg getAjaxMsg() {
		return ajaxMsg;
	}

	/**
	 * @param ajaxMsg
	 *            the ajaxMsg to set
	 */
	public void setAjaxMsg(AjaxResultMsg ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

	public String getNamespace() {
		ActionMapping ationMapping = ServletActionContext.getActionMapping();
		return ationMapping.getNamespace();
	}

	public String getActionName() {
		ActionMapping ationMapping = ServletActionContext.getActionMapping();
		return ationMapping.getName();
	}

	public String getParametes(Map<?, ?> m) {
		if (m == null) {
			return "";
		}
		StringBuilder sb = new StringBuilder(m.toString());
		sb.deleteCharAt(sb.length() - 1);
		sb.deleteCharAt(0);
		return sb.toString();
	}

	private static String colname = "";// 保存选中字段
	private static int pageinfo = 0;// 保存分页号
	private static String ascOrDesc = "";// 顺序或逆序

	/**
	 * 点击列表字段排序
	 * 
	 * @return
	 */
	public void setSortInfo(String orderCol, int page, Map map) {
		if (orderCol != null && !StringConvertor.isEmpty(orderCol)) {
			map.put("orderCol", orderCol);// 设定排序字段

			if (!colname.equals(orderCol)) {// 首次排序
				colname = orderCol;
				ascOrDesc = "ASC";// 顺序
			} else {// 非首次排序
				if (pageinfo == page) {// 停留在当前页
					ascOrDesc = (ascOrDesc.equals("ASC")) ? "DESC" : "ASC";// 切换顺序和逆序
				} else {
					pageinfo = page;// 获取分页号
				}
			}
			map.put("ascOrDesc", ascOrDesc);// 设定排序方式
		}
	}

	public List<TableForPatient> getTableForEnter(Long patientId, Integer flag) {
		try {
			tbList = commonService.getTableByPatientId(patientId, flag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbList;
	}

	public static String getAscOrDesc() {
		return ascOrDesc;
	}

	public static void setAscOrDesc(String ascOrDesc) {
		BaseAction.ascOrDesc = ascOrDesc;
	}

	public List<TableForPatient> getTbList() {
		return tbList;
	}

	public void setTbList(List<TableForPatient> tbList) {
		this.tbList = tbList;
	}

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
}
